﻿using AutoMapper;
using Dto;
using Microsoft.AspNetCore.Mvc;
using MyProject;

namespace Controllers
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SceneController : BaseController
    {
      
        private IFreeSql _fsql;
        private readonly IMapper _mapper;

        public SceneController(IFreeSql fsql, IMapper mapper)
        {
            _fsql = fsql;
            _mapper = mapper;
        }
        /// <summary>
        /// 场景列表
        /// </summary>
        [HttpPost]
        //[Authorize(Roles ="user")] 
        public ApiModel all()
        {
            var t1 = _fsql.Select<Scene>().ToList();
            if (t1 == null)
            {
                return apiModel(Status.Error, "无数据");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        /// <summary>
        /// 场景详情
        /// </summary>
        [HttpPost]
        //[Authorize(Roles ="user")] 
        public ApiModel detail([FromBody] ParmSceneId Id )
        {
            var t1 = _fsql.Select<Scene>().Where(a=>a.SceneId == Id.SceneId).First();
            if (t1 == null)
            {
                return apiModel(Status.Error, "无数据");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        /// <summary>
        /// 保存场景
        /// </summary>
        public ApiModel Save([FromBody] Scene data)
        {
            var t1 = _fsql.InsertOrUpdate<Scene>().SetSource(data).ExecuteAffrows();
            if (t1 == 0)
            {
                return apiModel(Status.Error, "保存成功");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        /// <summary>
        /// 删除场景
        /// </summary>
        public ApiModel Delete([FromBody] ParmId data)
        {
            var t1 = _fsql.Delete<Scene>(new { Id = data.Id }).ExecuteAffrows();
            if (t1 == 0)
            {
                return apiModel(Status.Error, "删除错误");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        /// <summary>
        /// 空间点赞填1，取消点赞填0，收藏一样
        /// </summary>
        public ApiModel Like([FromBody] UserScene data)
        {
            string[] usearray = null;
            string[] like = new []{ "Like" };
            string[] Favorite = new []{ "Favorite" };
            if (data.Like != null)
            {
                usearray = like;
                
                if (data.Like == 1)
                {
                    //累加总点赞数
                    _fsql.Update<Scene>(1).Set(a => a.LikesTotal + 1).ExecuteAffrows();
                }else if (data.Like == 0)
                {
                    _fsql.Update<Scene>(1).Set(a => a.LikesTotal - 1).ExecuteAffrows();
                }
            }
            else if (data.Favorite != null)
            {
                usearray = Favorite;

                if (data.Favorite == 1)
                {
                    _fsql.Update<Scene>(1).Set(a => a.FavoriteTotal + 1).ExecuteAffrows();
                }
                else if (data.Favorite == 0)
                {
                    _fsql.Update<Scene>(1).Set(a => a.FavoriteTotal - 1).ExecuteAffrows();
                }
            }

            //更新关系表
            var t = _fsql.InsertOrUpdate<UserScene>().SetSource(data).UpdateColumns(usearray).ExecuteAffrows();
            
            if (t == 0)
            {
                return apiModel(Status.Error, "Like错误");
            }
            else
            {
                return apiModel(Status.Success, "", t);
            }
        }
        /// <summary>
        /// 查询某用户是否针对某场景点赞收藏
        /// </summary>
        public ApiModel LikeGet([FromBody] LikeCDto data)
        {
            var t = _fsql.Select<UserScene>().Where(a=>a.UserId == data.UserId && a.SceneId == data.SceneId).ToList();
            if (t.Count()<0)
            {
                return apiModel(Status.Error, "No data");
            }
            else
            {
                return apiModel(Status.Success, "", t[0]);
            }
        }
    }
}
